package com.tmc.web.controller.intair;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.bean.intair.EthShoppingReq;
import com.tmc.constant.OrderConstant;
import com.tmc.entity.intair.IntairOrder;
import com.tmc.service.intair.IntairOrderServiceImpl;
import com.tmc.util.ObjectUtil;
import com.tmc.vendor.asms.util.Asms10000Util;
import com.tmc.web.BaseController;



@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
	private IntairOrderServiceImpl IntairOrderService;
	
	
	/**
	 * 跳转订单列表
	 * 
	 * @param req
	 * @return 
	 * 
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public ModelAndView findList(IntairOrder intairOrder,Integer pageIndex, Integer pageSize,
		String orderStatusTemp,HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		//System.out.println(intairOrder.getOrderStatus());
		
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<IntairOrder> page = IntairOrderService.findByCon(context, intairOrder, pageIndex, pageSize);
		map.put("page", page);
		map.put("intairOrder", intairOrder);
		map.put("orderStatusTemp", orderStatusTemp);
		map.put("context", context);
		return new ModelAndView("views/order/order-list", map);		
	}
	
	
	/**
	 * 跳转订单详情页面
	 * 
	 * @param orderId
	 * @return
	 */	
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public ModelAndView detail(Long orderId, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		IntairOrder order = IntairOrderService.getById(context, orderId);
		Map<String, Object> map= new HashMap<String, Object>();
		Map<String, Object> passengerMap= IntairOrderService.beforeShowPassenger(order.getPassengers());
		map.put("order", order);
		map.put("passengerMap", passengerMap);
		map.put("context", context);
		return new ModelAndView("views/order/order-detail",map);		
	}
	
	
	@RequestMapping(value="/selectCommision",method=RequestMethod.GET)
	public ModelAndView selectCommision(Long orderId, String policyIds, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map= new HashMap<String, Object>();
		IntairOrder order = IntairOrderService.selectCommision(context, orderId, policyIds);
		Map<String, Object> passengerMap= IntairOrderService.beforeShowPassenger(order.getPassengers());
		map.put("order", order);
		map.put("passengerMap", passengerMap);
		map.put("context", context);
		return new ModelAndView("views/order/order-detail",map);
	}
	
	/**
	 * 跳转退票、改签、废票和发送行程单页面
	 * 
	 * @param orderId
	 * @param flag
	 * @return
	 */
	@RequestMapping(value="/aftersale",method = RequestMethod.GET)
	public ModelAndView aftersale(Long orderId,String flag, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		IntairOrder order = IntairOrderService.getById(context, orderId);
		map.put("orderId", orderId);
		map.put("order", order);
		map.put("context", context);
		if(flag.equals("tuipiao")){
			return new ModelAndView("views/order/order-detail-tuipiao",map);
		}else if(flag.equals("gaiqi")){
			return new ModelAndView("views/order/order-detail-gaiqi",map);	
		}else if(flag.equals("feipiao")){
			return new ModelAndView("views/order/order-detail-feipiao",map);			
		}else{
			//跳转发送行程单页面
			return new ModelAndView("views/order/order-detail-xingchengdan",map);
		}			
	}
	

	 /**
	  * 跳转交易明细页面
	  * 
	  * @param orderId
	  * @param orderStatus
	  * @return
	  */	
	@RequestMapping(value="/tradeDetail",method = RequestMethod.GET)
	public ModelAndView tradeDetail(Long orderId,String orderStatus, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		String message="";
		if(orderStatus.equals("") || orderStatus.equals("1") || orderStatus.equals("2") || 
			orderStatus.equals("5")){
			message="暂无交易记录";
		}else{
			IntairOrder order = IntairOrderService.getById(context, orderId);
			map.put("order", order);
		}
		map.put("message", message);
		map.put("context", context);
		return new ModelAndView("views/order/order-detail-tradedetail",map);		
	}
	
	/**
	 * 跳转创建订单页面
	 * 
	 * @return
	 */
	@RequestMapping(value="/create",method = RequestMethod.GET)
	public ModelAndView create(HttpServletRequest request){
	
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("context", context);
		map.put("req", new EthShoppingReq());
		return new ModelAndView("views/order/create-order", map);
		
		
	}
	
	/**
	 * 通过PNR保存订单
	 * 
	 * @param pnr
	 * @return
	 */
	@RequestMapping(value="/saveByPnr",method = RequestMethod.POST)
	@ResponseBody
	public String saveByPnr(String pnr, Integer consumerClass, String personInfo, String officeNo, Long clientId) {
		CommonResult cr = null;
		try {
			IntairOrder order = Asms10000Util.getQTE(pnr);
			if (order == null || order.getFlights() == null || order.getFlights().size() <= 0
					|| order.getPassengers() == null || order.getPassengers().size() <= 0) {
				// 如果order为空或者航程为空则pnr无效
				cr = CommonResult.makeFail("无效的pnr");
				return cr.toJson();
			}
			order.setPnr(pnr);
			order.setOfficeNo(officeNo);
			// 设置
			// PNRCreateOrderUtil.setOrderStatusAndSource(order);

			if (consumerClass == 0) { // 0或者1
				// 商业伙伴
				order.setClientId(clientId);
			}
			cr = IntairOrderService.saveByPnr(new ContextInfo(), order);
			if (cr.isSuccess()) {
				cr.setObj(order.getOrderId());
				cr.setMessage("pnr创建订单成功");
			}
			return cr.toJson();
		} catch (Exception e) {
			logger.error("order保存出错", e);
			cr = CommonResult.makeFail("订单保存出错！");
		} finally {
			
		}
		return cr.toJson();
	}
	
	@RequestMapping(value="/saveByRt",method = RequestMethod.POST)
	@ResponseBody
	public String saveByRt(Integer consumerClass, String personInfo, String officeNo, Long clientId) {
		CommonResult cr = CommonResult.makeSuccess();
		return cr.toJson();
	}
	
	/**
	 * 审核通过
	 * 
	 * @param order
	 * @param policyId
	 * @return
	 */
	@RequestMapping(value="/checked")
	@ResponseBody
	public String checked(Long orderId, String policyIds){
		CommonResult cr=null;
		if(orderId == null || ObjectUtil.noText(policyIds)){
			cr = CommonResult.makeFail("审核时出错！");
			return cr.toJson();
		}		
		cr = IntairOrderService.orderChecked(new ContextInfo(), orderId, policyIds);
		cr.setMessage("审核已通过！");
		return cr.toJson();		
	}
	/**
	 * 审核拒回
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value="/unchecked",method=RequestMethod.GET)
	@ResponseBody
	public String unchecked(IntairOrder order){
		CommonResult cr=null;
		if(order == null || order.getOrderId() == null){
			cr = CommonResult.makeFail("拒回失败！");
			return cr.toJson();
		}
		order.setOrderStatus(OrderConstant.ORDER_STATUS_REFUSED);
		cr = IntairOrderService.updateOrderStatus(new ContextInfo(), order, "订单审核拒回成功");
		cr.setMessage("成功拒回！");
		return cr.toJson();		
	}
	
	@RequestMapping(value="/ticketing",method=RequestMethod.GET)
	@ResponseBody
	public String ticketing(String detailTextarea, Long orderId){
		CommonResult cr = CommonResult.makeFail("");
		return cr.toJson();
	}

}
